//
//  ViewController3.m
//  PASystem
//
//  Created by michael on 16/7/19.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "LockController.h"
#import "mimaViewController.h"
#import "OALoginViewController.h"
#import "ViewController3.h"
#import "SelectAlert.h"
#import <QuickLook/QuickLook.h>

@interface ViewController3 ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIWebViewDelegate>{
    NSInteger selectIndex;
        NSArray *titles;

}

@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
 UIColor *myColor = [UIColor colorWithWhite:0.8 alpha:0.3];
  _web = [[UIWebView alloc]initWithFrame:self.view.frame];
    _web.delegate = self;
//    _web.backgroundColor = [UIColor clearColor];
    _web.hidden = YES;
    [self.view addSubview:_web];

    self.view.backgroundColor = [UIColor whiteColor];
_view0 = [[UIView alloc]initWithFrame:self.view.frame];
_view0.backgroundColor = myColor;
[self.view addSubview:_view0];
    self.title = @"个人中心";
  if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
}
   
   }
- (void)viewWillAppear:(BOOL)animated{

// [self request];
        [self loaddata];

       [_view3 reloadData];


}
- (void)loaddata{


    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    [HttpTools requestWithURL:KExchmain arrays:array method:@"getFullName" success:^(id JSON) {
    NSLog(@"-------%@",JSON);
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

       _name = JSON[@"fullname"];
       [self initview];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self initview];
    }];
    



}

- (void)initview{

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, (KScreenHeight-26)/2)];
view.backgroundColor = [UIColor blueColor];
_image = [[UIImageView alloc]initWithFrame:CGRectMake((KScreenWidth-100)/2, (view.height-100)/2-40, 100, 100)];
if (  [[[NSUserDefaults standardUserDefaults]objectForKey:@"sex"] isEqualToString:@"N"]
){

_image.image = [UIImage imageNamed:@"头像-女"];

}else{
_image.image = [UIImage imageNamed:@"头像-男"];
}
//image.backgroundColor = [UIColor whiteColor];
[view addSubview:_image];
UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo)];

    [view addGestureRecognizer:tapGesture];


UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((KScreenWidth-80)/2,_image.bottom+10, 80, 30)];
//       label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];

label.text = @"欢迎您";
     label.font = [UIFont systemFontOfSize:15];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
[view addSubview:label];

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake((KScreenWidth-80)/2,label.bottom+10, 80, 30)];
label1.text = _name;
     label1.font = [UIFont systemFontOfSize:13];
label1.textAlignment = NSTextAlignmentCenter;
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
[view addSubview:label1];


_view3 = [[UITableView alloc]initWithFrame:CGRectMake(5, view.bottom+10, KScreenWidth-10, 200) style:UITableViewStylePlain];
_view3.dataSource = self;
_view3.delegate = self;
self.view3.bounces = NO;
_view3.backgroundColor = [UIColor whiteColor];

_view3.separatorStyle = NO;

[_view0 addSubview:view];
[_view0 addSubview:_view3];
}
// 选择男女图片
- (void)Actiondo{
    titles = @[@"设置性别为男",@"设置性别为女"];
[SelectAlert showWithTitle:@"设置性别" titles:titles selectIndex:^(NSInteger selectIndex) {
    } selectValue:^(NSString *selectValue) {
    NSLog(@"%@",selectValue);
if ([selectValue isEqualToString:@"设置性别为男"]) {
[[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"sex"];
[self viewWillAppear:YES];

}else{
//
[[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"sex"];
[self viewWillAppear:YES];

}


} showCloseButton:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row ==0) {
    
     UITableViewCell* cell = [[UITableViewCell alloc]init];
      UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,cell. contentView.height-20, cell. contentView.height-20)];
    image.image = [UIImage imageNamed:@"版本信息"];
[cell.contentView addSubview:image];

      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right +10, 0, 100, cell.contentView.height)];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    label.text = @"版本号:";
    
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
     NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  UILabel *label2= [[UILabel alloc]initWithFrame:CGRectMake(label.right, label.top, KScreenWidth  -150-image.width, label.height)];
    label2.text = appVersion;
      label2.textAlignment = NSTextAlignmentRight;

     label2.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:label2];
    return cell;

}if (indexPath.row ==1){
    
     UITableViewCell* cell = [[UITableViewCell alloc]init];
       UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,cell. contentView.height-20, cell. contentView.height-20)];
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头  

    image.image = [UIImage imageNamed:@"用户指南"];
[cell.contentView addSubview:image];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right +10, 0, 100, cell.contentView.height)];
    label.text = @"用户指南:";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
  UILabel *label2= [[UILabel alloc]initWithFrame:CGRectMake(label.right, label.top, KScreenWidth  -130-image.width, label.height)];
//    label2.text = [NSString stringWithFormat:@"%@",_dic[@"OFFICE"]];
      label2.textAlignment = NSTextAlignmentRight;

     label2.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:label2];
    return cell;

}if (indexPath.row ==2){
 UITableViewCell* cell = [[UITableViewCell alloc]init];
  UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,cell. contentView.height-20, cell. contentView.height-20)];
    image.image = [UIImage imageNamed:@"修改密码"];
[cell.contentView addSubview:image];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头  

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right +10, 0, 100, cell.contentView.height)];
    label.text = @"修改登录密码";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];

    return cell;


}
if (indexPath.row ==3){
 UITableViewCell* cell = [[UITableViewCell alloc]init];
  UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,cell. contentView.height-20, cell. contentView.height-20)];
    image.image = [UIImage imageNamed:@"手势密码"];
[cell.contentView addSubview:image];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头  

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right +10, 0, 100, cell.contentView.height)];
    label.text = @"手势设置";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];

    return cell;


}else{
 UITableViewCell* cell = [[UITableViewCell alloc]init];
  UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,cell. contentView.height-20, cell. contentView.height-20)];
    image.image = [UIImage imageNamed:@"归属公司"];
[cell.contentView addSubview:image];
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头  

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right +10, 0, 100, cell.contentView.height)];
    label.text = @"退出";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];

    return cell;


}



}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row ==1) {


    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    [HttpTools requestWithURL:KExchmain arrays:array method:@"helpDoc" success:^(id JSON) {
    NSLog(@"-------%@",JSON);
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSArray *array = JSON[@"data"];
NSDictionary *dic = [array firstObject];
_attacmentURL = dic[@"downlink"];
        [self openFileURL:_attacmentURL];

    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
      
    }];
  }


if (indexPath.row ==2) {
    mimaViewController *vc = [[mimaViewController alloc]init];
    [self.navigationController pushViewController: vc animated:YES];
}

if (indexPath.row ==4) {
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];

}
if (indexPath.row ==3) {
    titles = @[@"设置手势密码",@"关闭手势密码"];
[SelectAlert showWithTitle:@"设置手势" titles:titles selectIndex:^(NSInteger selectIndex) {
    } selectValue:^(NSString *selectValue) {
    NSLog(@"%@",selectValue);
if ([selectValue isEqualToString:@"设置手势密码"]) {
[[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"shoushi"];

    LockController *lockController = [[LockController alloc]init];
    lockController.type = @"设置";
    [self presentViewController:lockController animated:YES completion:nil];

}else{
//用车审批
[[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"shoushi"];

}


} showCloseButton:YES];
}

}
-(void)openFileURL:(NSString*)url{
    NSString * stringurl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.attacmentURL =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]]; //附件路径
 
    if ([self isExtistFilePathWithFileName:[[NSURL URLWithString:stringurl] lastPathComponent]]) { //已经存在附件
        QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
        previewoCntroller.delegate=self;
        previewoCntroller.dataSource=self;
        previewoCntroller.navigationItem.rightBarButtonItem=nil;
        [self.navigationController pushViewController: previewoCntroller animated:YES];
        
    }else{
        MBProgressHUD * hud  =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"下载中...";
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringurl]];
        [self.web loadRequest:request];
    }
    
}

#pragma mark PreviewDataSource
//第一个方法是设定一组文档浏览的个数，会影响到在文档预览中的上/下一页。
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
//第二个方法是指定文档预览的文件路径
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    
    return [NSURL fileURLWithPath:self.attacmentURL];
}

-(BOOL)isExtistFilePathWithFileName:(NSString*)fileName{
    NSString * file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString * filePath = [file stringByAppendingPathComponent:fileName];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath] && fileName.length != 0) {
        return YES;
    }else{
        return NO;
    }
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSURL * requestedURL = [request URL];
    NSData * fileData = [[NSData alloc] initWithContentsOfURL:requestedURL];
    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     [fileData writeToFile:[filePath stringByAppendingPathComponent:[requestedURL lastPathComponent]] atomically:YES];
      return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
    previewoCntroller.delegate=self;
    previewoCntroller.dataSource=self;
    [self.navigationController pushViewController: previewoCntroller animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){
// [_helper logoutVpn];
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 OALoginViewController * loginVC =  [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
    self.view.window.rootViewController = loginVC;


}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
